package com.aiwei.tdjk.service.impl;

import com.aiwei.tdjk.bean.Pager;
import com.aiwei.tdjk.dao.extend.RoleExtendMapper;
import com.aiwei.tdjk.dto.request.GetRolePagerRequest;
import com.aiwei.tdjk.entity.Role;
import com.aiwei.tdjk.entity.RoleExample;
import com.aiwei.tdjk.entity.RoleResourcePermission;
import com.aiwei.tdjk.service.RoleResourcePermissionService;
import com.aiwei.tdjk.service.RoleService;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * Created by yanfeng on 2016/12/13.
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleExtendMapper roleExtendMapper;

    @Autowired
    private RoleResourcePermissionService roleResourcePermissionService;


    /**
     * 获取可用的角色列表
     *
     * @param roleIds
     * @return
     */
    @Override
    public Set<Role> findShowRoles(Set<Long> roleIds) {

        Set<Role> roles = Sets.newHashSet();
        //TODO 如果角色很多 此处应该写查询
        for (Role role : roleExtendMapper.selectByExample(new RoleExample())) {
            if (Boolean.TRUE.equals(role.getIsShow()) && roleIds.contains(role.getId())) {
                roles.add(role);
            }
        }
        return roles;
    }


    @Override
    public Role get(Long id) {
        return roleExtendMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> list() {
        return roleExtendMapper.selectByExample(new RoleExample());
    }

    /**
     * 根据roleId获取角色列表
     *
     * @param roleIds (1,2,3)
     * @return
     */
    @Override
    public List<Role> getByRoleIds(String roleIds) {

        if (StringUtils.isEmpty(roleIds))
            return new ArrayList<>();
        String[] roleId_Array = roleIds.split(",");
        Long[] roleId_Long  = new Long[roleId_Array.length];

        for (int i = 0;i<roleId_Array.length;i++){
            roleId_Long[i]=Long.parseLong(roleId_Array[i]);
        }
        RoleExample example = new RoleExample();
        example.or().andIdIn(Arrays.asList(roleId_Long));
        return roleExtendMapper.selectByExample(example);
    }

    @Override
    public void save(Role role) {
        roleExtendMapper.insertSelective(role);
    }

    @Override
    public void save(Role role, Long[] resourceId, String[] permissionIds) {
        save(role);
        saveRoleResourcePermission(role, resourceId, permissionIds);
    }

    @Override
    public int update(Role role) {
        return roleExtendMapper.updateByPrimaryKey(role);
    }

    @Override
    public int update(Role role, Long[] resourceId, String[] permissionIds) {
        //1.删除角色对应的资源权限；
        roleResourcePermissionService.deleteResourcePermissionsByRoleId(role.getId());
        //2、添加角色对应的资源权限；
        saveRoleResourcePermission(role, resourceId, permissionIds);
        return roleExtendMapper.updateByPrimaryKeySelective(role);
    }

    private void saveRoleResourcePermission(Role role, Long[] resourceId, String[] permissionIds) {
        if (resourceId != null) {
            for (int i = 0; i < resourceId.length; i++) {
                RoleResourcePermission rRp = new RoleResourcePermission();
                rRp.setPermissionIds(permissionIds[i]);
                rRp.setResourceId(resourceId[i]);
                rRp.setRoleId(role.getId());
                roleResourcePermissionService.save(rRp);
            }
        }
    }

    @Override
    public int delete(Long id) {
        return roleExtendMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int delete(Long[] ids) {
        if (ids != null && ids.length > 0) {
            for (Long id : ids) {
                delete(id);
            }
            return ids.length;
        }
        return 0;
    }

    /**
     * 获取分页列表
     *
     * @param request
     */
    @Override
    public Pager<Role> find(GetRolePagerRequest request) {
        request.webParamConvert();
        List<Role> roleList = roleExtendMapper.find(request);
        int totalCount = roleExtendMapper.getTotalCount(request);
        Pager<Role> pager = new Pager<>();
        pager.setPageNumber(request.getPageNumber());
        pager.setPageSize(request.getPageSize());
        pager.setList(roleList);
        pager.setTotalCount(totalCount);
        return pager;
    }

    @Override
    public List<Long> getRoleIdsByRoleSet(Set<Role> roles) {
        List<Long> roleIds = Lists.newArrayList();
        for(Role role:roles){
            roleIds.add(role.getId());
        }
        return roleIds;
    }
}
